package com.earogya.logic.patient_details;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.struts.ehr.patient_details.model.*;


import ca.uhn.hl7v2.HL7Exception;
import ca.uhn.hl7v2.model.DataTypeException;
import ca.uhn.hl7v2.model.v251.datatype.XAD;
import ca.uhn.hl7v2.model.v251.datatype.XPN;
import ca.uhn.hl7v2.model.v251.group.ADR_A19_QUERY_RESPONSE;
import ca.uhn.hl7v2.model.v251.message.ADR_A19;
import ca.uhn.hl7v2.model.v251.segment.AL1;
import ca.uhn.hl7v2.model.v251.segment.DB1;
import ca.uhn.hl7v2.model.v251.segment.PD1;
import ca.uhn.hl7v2.model.v251.segment.PID;


public class ADR_A19_Handler {
	private ADR_A19 adtMsg;
	private List<PatientDetails> patientDetailsList=new ArrayList<PatientDetails>();
		
	public ADR_A19_Handler(){
		
	}
	
	public ADR_A19_Handler(ADR_A19 adtMsg){
		this.adtMsg=adtMsg;
	}
	
	public List<PatientDetails> manageMessage() throws DataTypeException, HL7Exception{
			for(int i=0;i<adtMsg.getQUERY_RESPONSEReps();i++){
				patientDetailsList.add(new PatientDetails());
				ADR_A19_QUERY_RESPONSE response= adtMsg.getQUERY_RESPONSE(i);
				Patient patient =patientDetailsList.get(i).getPatient();
				PID pid=response.getPID();
			patient.setPatientId(Integer.parseInt(pid.getPatientID().getIDNumber().getValue()));
			patient.setPrefix(pid.getPatientName(0).getPrefixEgDR().getValue());
			patient.setGivenName(pid.getPatientName(0).getGivenName().getValue());
			patient.setFamilyName(pid.getPatientName(0).getFamilyName().getSurname().getValue());
			patient.setGender(pid.getAdministrativeSex().getValue());
			patient.setNicNo(pid.getPatientIdentifierList(0).getIDNumber().getValue());
			if(pid.getDateTimeOfBirth().getTime().getValue()==null){}
			else{
				patient.setBirthTime(pid.getDateTimeOfBirth().getTime().getValueAsDate());
			}
			patient.setBirthPlaceAddress(pid.getBirthPlace().toString());
			patient.setMaritialStatus(pid.getMaritalStatus().getText().toString());
			patient.setEthnicGroupId(pid.getEthnicGroup(0).getText().toString());
			patient.setReligiousAffliationId(pid.getReligion().getText().toString());
			
		}
			this.enterPatientAddressDetails();
			this.enterPatientEmailAndTelephoneDetails();
			return patientDetailsList;
	}
	
	
	
	private void enterPatientAddressDetails()throws HL7Exception{
		for(int i=0;i<adtMsg.getQUERY_RESPONSEReps();i++){
		ADR_A19_QUERY_RESPONSE response= adtMsg.getQUERY_RESPONSE(i);
		PID pid=response.getPID();
		PatientAddress patientAddress=patientDetailsList.get(0).getPatientAddressList();
		
			patientDetailsList.get(i).getPatientAddressList().setPatientId(this.patientDetailsList.get(i).getPatient().getPatientId());
			patientAddress.setCity1(pid.getPatientAddress(0).getOtherDesignation().getValue());
			patientAddress.setCountry(pid.getPatientAddress(0).getCountry().getValue());
			patientAddress.setHouseNumber(pid.getPatientAddress(0).getStreetAddress().getDwellingNumber().getValue());
			patientAddress.setCity2(pid.getPatientAddress(0).getCity().getValue());
			patientAddress.setStreet(pid.getPatientAddress(0).getStreetAddress().getStreetName().getValue());
			System.out.print(pid.getPatientAddress(0).getStreetAddress().getDwellingNumber().getValue());
			
		}
		}
	
	/*private void enterPatientDisabilityDetails() throws DataTypeException{
		DB1 db1;
		int db1Repetitions=adtMsg.getDB1Reps();
		for(int i=0;i<db1Repetitions;i++){
			db1=adtMsg.getDB1(i);
			pDisability=new PatientDisability();
			pDisability.setPatientId(this.patient.getPatientId());
			pDisability.setDisabilityId(Integer.parseInt(db1.getDisabledIndicator().getValue()));
			if(db1.getDisabilityStartDate()==null){}
			else{
				pDisability.setDisabilityStartDate(this.dtToDate(db1.getDisabilityStartDate()));
			}
			if(db1.getDisabilityEndDate()==null){}
			else{
				pDisability.setDisabilityEndDate(this.dtToDate(db1.getDisabilityEndDate()));
			}	
			hibernateManager.saveObject(pDisability);
		}
	}*/
	
	private void enterPatientEmailAndTelephoneDetails(){
		for(int j=0;j<adtMsg.getQUERY_RESPONSEReps();j++){
			ADR_A19_QUERY_RESPONSE response= adtMsg.getQUERY_RESPONSE(j);
			PID pid=response.getPID();
		int i=0;
		while(!(pid.getPhoneNumberHome(i).getTelephoneNumber().getValue()==null)||!(pid.getPhoneNumberHome(i).getEmailAddress().getValue()==null)){
			
			if(pid.getPhoneNumberHome(i).getTelephoneNumber().getValue()==null){}
			else{
			patientDetailsList.get(j).getPatientTelephoneList().add(new PatientTelephone());
				patientDetailsList.get(j).getPatientTelephoneList().get(i).setPatientId(this.patientDetailsList.get(j).getPatient().getPatientId());
				patientDetailsList.get(j).getPatientTelephoneList().get(i).setTelephoneNo(pid.getPhoneNumberHome(i).getTelephoneNumber().getValue());
			}
			if(pid.getPhoneNumberHome(i).getEmailAddress().getValue()==null){}
			else{
				patientDetailsList.get(j).getPatientEmailList().add(new PatientEmail());
				patientDetailsList.get(j).getPatientEmailList().get(i).setPatientId(this.patientDetailsList.get(j).getPatient().getPatientId());
				patientDetailsList.get(j).getPatientEmailList().get(i).setEmail(pid.getPhoneNumberHome(i).getEmailAddress().getValue());
			}
			i++;
		}
		}
	
	}
	
	/*private void entereAllergyDetails() throws DataTypeException{
		AL1 al1;
		for(int i=0;i<adtMsg.getAL1Reps();i++){
			al1=adtMsg.getAL1(i);
			pAllergy=new PatientAllergy();
			pAllergy.setAllergyCode(al1.getAllergenCodeMnemonicDescription().getComponent(0).toString());
			pAllergy.setPatientId(this.patient.getPatientId());
			pAllergy.setSeverity(al1.getAllergySeverityCode().getComponent(0).toString());
			pAllergy.setIdentificationDate(this.dtToDate(al1.getAl16_IdentificationDate()));
			System.out.print(al1.getAllergenCodeMnemonicDescription().getComponent(0).toString());
			hibernateManager.saveObject(pAllergy);
		}
	}*/
	
}
